package sg.edu.nus.iss.billing.gui.component;

import javax.swing.JTextField;

import sg.edu.nus.iss.billing.exception.ValidationException;
import sg.edu.nus.iss.billing.validation.FormValidator;
import sg.edu.nus.iss.billing.validation.ValidationRule;

public class ValidateJTextField extends JTextField {
	
	private ValidationRule[] rules;
	private String fieldName;
	private boolean isUpperCase;
	public ValidateJTextField(String fieldName, ValidationRule ... rules){
		this(fieldName, false, rules);
	}
	
	public ValidateJTextField(String fieldName, boolean isUpperCase, ValidationRule ... rules){
		this.fieldName = fieldName;
		this.rules = rules;
	}
	
	public void setFieldName(String myFieldName) {
		this.fieldName = myFieldName;
	}
	
	public void setRules(ValidationRule ... rules) {
		this.rules = rules;
	}
	
	public String getValidatedText() throws ValidationException{
		if(this.isEditable())
			FormValidator.validate(this.fieldName, this.getText(), rules);
		if(isUpperCase)
			return this.getText().toUpperCase();
		else
			return this.getText();
	}
}